﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="SessionRunspaceFactory.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell
{
    using System;
    using System.Security;
    using Microsoft.LiveAtEdu.Samples.Powershell.OnPremise;
    using Microsoft.LiveAtEdu.Samples.Powershell.OutlookLive;

    /// <summary>
    /// Responsible for creating SessionRunspace based on the session types.
    /// </summary>
    public static class SessionRunspaceFactory
    {
        /// <summary>
        /// Creates new instance of SessionRunspace based on the session type.
        /// </summary>
        /// <param name="sessionType">Type of session.</param>
        /// <param name="userName">The user name to authenticate the session.</param>
        /// <param name="password">The password to authenticate the session.</param>
        /// <param name="connectionUri">Uri for creating the connection.</param>
        /// <returns>Instance of SessionRunspace.</returns>
        /// <remarks>
        /// If a non supported session type is passed, this will return null.
        /// </remarks>
        public static SessionRunspace CreateInstance(SessionType sessionType, string userName, SecureString password, string connectionUri)
        {
            // Validate the inputs (Throw ArgumentOutOfRangeException for all invalid inputs).
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentOutOfRangeException("userName");
            }

            if (password == null)
            {
                throw new ArgumentOutOfRangeException("password");
            }

            if (string.IsNullOrEmpty(connectionUri))
            {
                throw new ArgumentOutOfRangeException("connectionUri");
            }

            switch (sessionType)
            {
                case SessionType.OnPremiseSession:
                    return new OnPremiseSessionRunspace(userName, password, connectionUri);
                case SessionType.OutlookLiveSession:
                    return new OutlookLiveSessionRunspace(userName, password, connectionUri);
                default:
                    return null;
            }
        }
    }
}
